package com.ihteima.pojo;

import com.ihteima.exception.BaseExceptionInterface;
import com.ihteima.exception.BizException;
import lombok.Data;

import java.io.Serializable;

/**
 * ClassName: Result
 * Package: com.ihteima.pojo
 * Description:
 *
 * @Auther gongkaiming
 * @Create 2025/10/26 17:37
 * @Version 1.0
 */
@Data
public class Response<T> implements Serializable {

    private String code;
    private Boolean isSuccess = true;
    private String message;
    private T data;

    // 成功响应
    public static <T> Response<T> success() {
        Response<T> response = new Response<>();
        return response;
    }

    public static <T> Response<T> success(T data) {
        Response<T> response = new Response<>();
        response.setData(data);
        return response;
    }

    // 失败响应
    public static <T> Response<T> fail() {
        Response<T> response = new Response<>();
        response.setIsSuccess(false);
        return response;
    }

    public static <T> Response<T> fail(String errorMessage) {
        Response<T> response = new Response<>();
        response.setIsSuccess(false);
        response.setMessage(errorMessage);
        return response;
    }

    public static <T> Response<T> fail(String errorCode, String errorMessage) {
        Response<T> response = new Response<>();
        response.setIsSuccess(false);
        response.setCode(errorCode);
        response.setMessage(errorMessage);
        return response;
    }

    public static <T> Response<T> fail(BizException bizException) {
        Response<T> response = new Response<>();
        response.setIsSuccess(false);
        response.setCode(bizException.getErrorCode());
        response.setMessage(bizException.getErrorMessage());
        return response;
    }

    public static <T> Response<T> fail(BaseExceptionInterface baseExceptionInterface) {
        Response<T> response = new Response<>();
        response.setIsSuccess(false);
        response.setCode(baseExceptionInterface.getErrorCode());
        response.setMessage(baseExceptionInterface.getErrorMessage());
        return response;
    }

    public boolean isSuccess() {
        return isSuccess != null && isSuccess;
    }

}
